package com.train.system.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.system.resp.train.TrainCodeResp;
import com.train.system.resp.train.TrainDetailResp;
import com.train.system.resp.train.TrainListResp;
import com.train.system.resp.train.TrainPageResp;
import com.train.system.dto.train.TrainCodeDTO;
import com.train.system.dto.train.TrainPageDTO;
import com.train.system.po.TrainPO;
import com.train.mybatis.base.MyBaseMapper;

import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【train(车次)】的数据库操作Mapper
 * @createDate 2025-02-20 01:04:22
 * @Entity generator.domain.Train
 */
public interface TrainMapper extends MyBaseMapper<TrainPO> {

    default PageResp<TrainPageResp> pageList(TrainPageDTO trainPageDTO) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(trainPageDTO.getCode())) {
            queryWrapper.like(TrainPO::getCode, trainPageDTO.getCode());
        }
        PageResp<TrainPO> trainPOPageResp = this.selectPage(
                trainPageDTO,
                queryWrapper, new PageSorted(TrainPO::getUpdateTime), new PageSorted(TrainPO::getCreateTime));
        List<TrainPageResp> trainPageRespList = BeanUtil.copyToList(trainPOPageResp.getData(), TrainPageResp.class);
        return new PageResp<>(trainPOPageResp.getTotal(), trainPageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainPO::getId, id);
        return this.exists(queryWrapper);
    }

    default boolean existsCode(String code) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainPO::getCode, code);
        return this.exists(queryWrapper);
    }

    default PageResp<TrainCodeResp> pageCodeList(TrainCodeDTO trainCodeDTO) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainPO::getCode).like(TrainPO::getCode, trainCodeDTO.getKeyWords());
        PageResp<TrainPO> trainPOPageResp = this.selectPage(
                trainCodeDTO, //分页
                queryWrapper, //条件
                new PageSorted(TrainPO::getCode, PageSorted.SortedType.ASC)); //排序
        List<TrainCodeResp> trainCodeRespList = BeanUtil.copyToList(trainPOPageResp.getData(), TrainCodeResp.class);
        return new PageResp<>(trainPOPageResp.getTotal(), trainCodeRespList);
    }

    default Long getId(String trainCode) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainPO::getId).eq(TrainPO::getCode, trainCode);
        return this.selectOne(queryWrapper).getId();
    }

    default TrainDetailResp getTrainDetail(String trainCode) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainPO::getCode, trainCode);
        TrainPO trainPO = this.selectOne(queryWrapper);
        return BeanUtil.copyProperties(trainPO, TrainDetailResp.class);
    }

    default List<TrainPageResp> selectTrainList(TrainPageDTO trainPageDTO) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainPO::getCode, TrainPO::getType, TrainPO::getStart, TrainPO::getStartPinyin,
                TrainPO::getStartTime, TrainPO::getEnd, TrainPO::getEndPinyin, TrainPO::getEndTime);
        PageResp<TrainPO> pageResp = this.selectPage(
                trainPageDTO,
                queryWrapper);
        return BeanUtil.copyToList(pageResp.getData(), TrainPageResp.class);
    }

    default List<TrainListResp> selectTrainListByTrainCodes(List<String> trainCodes) {
        LambdaQueryWrapper<TrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainPO::getCode, TrainPO::getType, TrainPO::getStart, TrainPO::getStartPinyin,
                TrainPO::getStartTime, TrainPO::getEnd, TrainPO::getEndPinyin, TrainPO::getEndTime);
        queryWrapper.in(TrainPO::getCode, trainCodes);
        return BeanUtil.copyToList(this.selectList(queryWrapper), TrainListResp.class);
    }
}